local Class = require('lua.lang.Class')

--- @class UpValue : Class
--- @field stack LuaValue[]
--- @field index number
local UpValue = Class {}

--- @param stack LuaValue[]
--- @param index number
--- @return UpValue
function UpValue:new(stack, index)
	--- @type UpValue
	local o = {}
	o.stack = stack
	o.index = index
	return setmetatable(o, UpValue)
end

--- @return LuaValue
function UpValue:getValue()
	return self.stack[self.index + 1]
end

--- @param value LuaValue
--- @return void
function UpValue:setValue(value)
	self.stack[self.index + 1] = value
end

--- @return void
function UpValue:close()
	local old = self.stack
	local i = self.index
	self.stack = { old[i] }
	self.index = 0
	old[i] = nil
end

return UpValue